<script setup lang="ts">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { Dialog } from '@/views/QuanXian/quanxianguanli/bumenguanli/bumen/Dialog'
import { useI18n } from '@/hooks/web/useI18n'
import { ElButton, ElTag,ElSwitch } from 'element-plus'
import { Table } from '@/components/Table'
import { getTableListApi,  delTableListApi} from '@/api/rizhiapi'
import { useTable } from '@/hooks/web/useTable'
import { TableData,TableDatatwo } from '@/api/table/types'
import { h, ref, unref, reactive } from 'vue'
import Write from './components/Write.vue'
import Detail from './components/Detail.vue'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { TableColumn } from '@/types/table'
import { FormSchema } from '@/types/form'
import { ElMessage } from 'element-plus'

const { register, tableObject, methods } = useTable<TableData>({
  getListApi: getTableListApi,
  delListApi: delTableListApi,
  response: {
    list: 'list',
    total: 'total'
  },
  defaultParams: {
    title: 's'
  }
})

const { getList, setSearchParams } = methods
getList()

const { t } = useI18n()

//表格的虚拟数据
const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'id',
    label: '序号',
    type: 'index',
  },
  {
    field: 'trueName',
    label:'管理员名称'
  },
  {
    field: 'tablename',
    label:'操作的列表',
  },
  {
    field: 'createDate',
    label:'执行时间',
    formatter: (row, column, cellValue, index) => {
      let format = 'YYYY-mm-dd HH:MM:SS'
      let date = new Date(cellValue);
      const dataItem = {
        'Y+': date.getFullYear().toString(),
        'm+': (date.getMonth() + 1).toString(),
        'd+': date.getDate().toString(),
        'H+': date.getHours().toString(),
        'M+': date.getMinutes().toString(),
        'S+': date.getSeconds().toString(),
      };
      Object.keys(dataItem).forEach((item) => {
        const ret = new RegExp(`(${item})`).exec(format);
        if (ret) {
          format = format.replace(ret[1], ret[1].length === 1 ? dataItem[item] : dataItem[item].padStart(ret[1].length, '0'));
        }
      });
      return format
    }
  },
  {
    field: 'type',
    label:'操作类型',
    formatter: (row, column, cellValue, index) => {
      if (cellValue == 2){
        return '添加'
      }else if(cellValue == 3){
        return '删除'
      }else if(cellValue == 4){
        return '修改'
      }else if(cellValue == 5){
        return '查找'
      }
    }
  },
  {
    field: 'remark',
    label:'备注',
    formatter: (row, column, cellValue, index) => {
      if (cellValue == null){
        return '无内容'
      }
    }
  },
  {
    field: 'action',
    width: '260px',
    label: t('tableDemo.action'),
    form: {
      show: false
    },
    detail: {
      show: false
    }
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)

const dialogVisible = ref(false)

const dialogTitle = ref('')

const delLoading = ref(false)

const delData = async (row: TableData | null, multiple: boolean) => {
  tableObject.currentRow = row
  console.log(tableObject.currentRow)
  const { delList, getSelections } = methods
  const selections = await getSelections()
  delLoading.value = true
  await delList(
    multiple ? selections.map((v) => v.id) : tableObject.currentRow?.id,
    multiple
  ).finally(() => {
    delLoading.value = false
  })
}

const actionType = ref('')

const writeRef = ref<ComponentRef<typeof Write>>()

const loading = ref(false)

allSchemas.searchSchema = reactive<FormSchema[]>([
  {
    field: 'trueName',
    label: '管理员名称',
    component: 'Input',
  },
  {
    field: 'tablename',
    label: '操作的列表',
    component: 'Input',
  },
  {
    field: 'type',
    label: '操作类型',
    component: 'Select',
    value:0,
    componentProps: {
      options: [
        {
          label:'添加',
          value:2
        },
        {
          label:'删除',
          value:3
        },
        {
          label:'修改',
          value:4
        },
        {
          label:'查找',
          value:5
        },
        {
          label:'全部',
          value:0
        }
      ]
    }
  },
  {
    field: 'createDate',
    component: 'DatePicker',
    label: '操作时间',
    componentProps: {
      type: 'date',
      valueFormat:'YYYY-MM-DD'
    }
  },
])
</script>

<template>
  <div class="pre_home">
    <div class="div_tit" style="margin-bottom: 20px">
      <div class="div_ic"></div>
      <p class="tit_span">操作日志</p>
    </div>
    <div class="div_index">
      <div class="div_index_title">
        <Icon icon="tabler:search" />
        筛选查询
      </div>
    </div>
  <ContentWrap>
    <div style="width: 2000px">
      <Search
              :model="{ title: '' }"
              :schema="allSchemas.searchSchema"
              @search="setSearchParams"
              @reset="setSearchParams"
      />
    </div>
  </ContentWrap>
  <ContentWrap>

    <Table
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :columns="allSchemas.tableColumns"
      :data="tableObject.tableList.slice((tableObject.currentPage - 1) * tableObject.pageSize,tableObject.currentPage * tableObject.pageSize)"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      @register="register"
    >
      <template #action="{ row }">
        <ElButton type="danger" v-hasPermi="['example:dialog:delete']" @click="delData(row, false)">
          {{ t('exampleDemo.del') }}
        </ElButton>
      </template>
    </Table>
  </ContentWrap>
  </div>
</template>

<style lang="less" scoped>
  .pre_home{
    width:100%;
    height:100%;
  }
  .div_tit{
    margin-left: 0.1%;
    width:99%;
    height:50px;
    background-color:#F2F2F2;
    border:2px solid #E6E6E6;
    position: relative;
  }
  .tit_span{
    margin-left:10px;
    margin-top:-20px;
    color:#999999;
  }

  .div_ic{
    width:5px;
    height:20px;
    background-color:#1ABC9C;
    margin-top:15px;
  }
  .div_btn{
    margin-left: 2.5%;
    margin-right:20px;
    width:96%;
    height:50px;
    margin-bottom:10px;
  }
  .btn_li_item{
    margin-top:13px;
    width:140px;
    float:left;
  }
  .div_btn_class{
    width:130px;
    height:35px;
    background-color: #31A99F;
    color: white;
    border: 1px solid black;
  }
  .div_btn_classtwo{
    width:130px;
    height:35px;
    background-color: white;
    color: black;
    border: 1px solid black;
  }
  .span_Num{
    color: #ff4624;
    font-weight: bold;
  }
  .div_index_ul1{
    overflow: hidden;
    width: 210px;
    position: absolute;
    right: -17px;
  }
  .div_index_title{
    width:100%;
    height:55px;
    background-color:#F3F3F3;
    border-bottom:1px solid #E6E6E6;
    font-weight:bold;
    line-height:55px;
    font-size:16px;
    text-indent:18px;
    color:#666666;
  }
</style>
